home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Text and Fonts / TextColumns / TextColumns.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  2.1 KB  |  63 lines

  1. //------------------------------------------
  2. // TextColumns.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Text;
  8. using System.Windows.Forms;
  9.  
  10. class TextColumns: PrintableForm
  11. {
  12.      public new static void Main()
  13.      {
  14.           Application.Run(new TextColumns());
  15.      }
  16.      public TextColumns()
  17.      {
  18.           Text = "Edith Wharton's \"The Age of Innocence\"";
  19.           Font = new Font("Times New Roman", 10);
  20.      }
  21.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  22.      {
  23.           Brush        brush  = new SolidBrush(clr);
  24.           int          iChars, iLines;
  25.           string       str    = AgeOfInnocence.Text;
  26.           StringFormat strfmt = new StringFormat();
  27.  
  28.                // Set units of points while converting dimensions.
  29.  
  30.           PointF[] aptf = { new PointF(cx, cy) };
  31.           grfx.TransformPoints(CoordinateSpace.Device, 
  32.                                CoordinateSpace.Page, aptf);
  33.  
  34.           grfx.PageUnit  = GraphicsUnit.Point;
  35.  
  36.           grfx.TransformPoints(CoordinateSpace.Page,
  37.                                CoordinateSpace.Device, aptf);
  38.           float fcx = aptf[0].X;
  39.           float fcy = aptf[0].Y;
  40.  
  41.                // StringFormat properties, flags, and tabs.
  42.  
  43.           strfmt.HotkeyPrefix = HotkeyPrefix.Show;
  44.           strfmt.Trimming     = StringTrimming.Word;
  45.           strfmt.FormatFlags |= StringFormatFlags.NoClip; 
  46.           strfmt.SetTabStops(0, new float[] { 18 });
  47.  
  48.                // Display text.
  49.  
  50.           for (int x = 0; x < fcx && str.Length > 0; x += 156)
  51.           {
  52.                RectangleF rectf = new RectangleF(x, 0, 144, 
  53.                                                  fcy - Font.GetHeight(grfx));
  54.  
  55.                grfx.DrawString(str, Font, brush, rectf, strfmt);
  56.                grfx.MeasureString(str, Font, rectf.Size, strfmt, 
  57.                                   out iChars, out iLines);
  58.  
  59.                str = str.Substring(iChars);
  60.           }
  61.      }
  62. }
  63.